//
//  ForgetPasswordViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/23.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "ForgetPasswordViewController.h"

@interface ForgetPasswordViewController ()
<
UITextFieldDelegate
>
{
    UITextField * phoneTextField;
    UITextField * codeTextField;
    UITextField * passTextField;
    UITextField * makePassTextField;
    
    UILabel * label;
    NSTimer * timer;
    NSInteger count;
    UIButton * codeBtn;
    UIScrollView * scrollView;
}

@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"忘记密码";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self initView];
    
}

- (void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)initView{
    
    scrollView=[[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIView *vContainer = [UIView new];
    [scrollView addSubview:vContainer];
    [vContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    
    
    
    UIImageView * midView = [[UIImageView alloc]init];
    [midView setUserInteractionEnabled:YES];
    [midView setImage:[UIImage imageNamed:@"bg_zuce_shuruz_bax"]];
    [vContainer addSubview:midView];
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vContainer).offset(62 * kAutoScanleX);
        make.centerX.equalTo(vContainer);
        make.size.mas_equalTo(CGSizeMake(345 * kAutoScanleX, 232 * kAutoScanleX));
    }];
    
    NSArray * pArray = @[@"请输入手机号",@"请输入验证码",@"输入新密码",@"确认新密码"];
    
    for (NSInteger i = 0; i < 4; i ++) {
        
        UIView * item = [[UIView alloc]init];
        [midView addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(midView).offset(8 * kAutoScanleX + i * 54 * kAutoScanleX);
            make.left.right.equalTo(midView).inset(7.5 * kAutoScanleX);
            make.height.mas_equalTo(54 * kAutoScanleX);
        }];
        
        
        UITextField * textField =[[UITextField alloc]init];
        [textField setDelegate:self];
        [textField setPlaceholder:pArray[i]];
        [item addSubview:textField];
        [textField setTextColor:HexRGBA(0x000000, 1)];
        [textField setReturnKeyType:UIReturnKeyDone];
        [textField setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
        [textField setValue:HexRGBA(0xa4a4a4, 1) forKeyPath:@"_placeholderLabel.textColor"];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(item);
            make.left.equalTo(item).offset(15 * kAutoScanleX);
            make.right.equalTo(item).offset(-15 * kAutoScanleX);
            make.height.mas_equalTo(40 * kAutoScanleX);
        }];
        
        
        if (i == 0) {
            
            phoneTextField = textField;
            
        }
        
        if (i == 1) {
            
            codeTextField = textField;
            [textField mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(item).offset(-100 * kAutoScanleX);
            }];
            
            label=[[UILabel alloc]init];
            [item addSubview:label];
            [label setText:@"获取验证码"];
            [label setBackgroundColor:HexRGBA(0xa4a4a4, 1)];
            [label setFont:[UIFont systemFontOfSize:11 * kAutoScanleX]];
            [label setTextColor:HexRGBA(0xffffff, 1)];
            [label setTextAlignment:NSTextAlignmentCenter];
            [label.layer setMasksToBounds:YES];
            [label.layer setCornerRadius:4 * kAutoScanleX];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(item);
                make.right.equalTo(item).offset(-15 * kAutoScanleX);
                make.size.mas_equalTo(CGSizeMake(70 * kAutoScanleX, 24 * kAutoScanleX));
            }];
            
            
            codeBtn = [[UIButton alloc]init];
            [codeBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [item addSubview:codeBtn];
            [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(label);
            }];
            
        }
        
        if (i == 0 | i == 1) {
            
            [textField setKeyboardType:UIKeyboardTypeNumberPad];
            
            UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth, 40)];
            customView.backgroundColor = [UIColor whiteColor];
            textField.inputAccessoryView = customView;
            
            UIButton *rightBtn=[[UIButton alloc]init];
            [rightBtn addTarget:self action:@selector(finishBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
            [customView addSubview:rightBtn];
            [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(customView);
                make.right.equalTo(customView).offset(-15);
                make.size.mas_equalTo(CGSizeMake(40, 40));
            }];
            
            
            UIView *line=nil;
            
            line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, customView.current_w, 0.5)];
            [customView addSubview:line];
            [line setBackgroundColor:HexRGBA(0xd3d5d5, 1)];
            
            line=[[UIView alloc]initWithFrame:CGRectMake(0, 39.5, customView.current_w, 0.5)];
            [customView addSubview:line];
            [line setBackgroundColor:HexRGBA(0xd3d5d5, 1)];
        }
        
        
        if (i == 2) {
            
            passTextField = textField;
            [textField setSecureTextEntry:YES];
            
            UIButton * secureButton = [[UIButton alloc]init];
            [secureButton setImage:[UIImage imageNamed:@"icon_mima_guanbi"] forState:UIControlStateNormal];
            [secureButton setImage:[UIImage imageNamed:@"icon_mima_zhankai"] forState:UIControlStateSelected];
            [secureButton addTarget:self action:@selector(buttonSecureAction:) forControlEvents:UIControlEventTouchUpInside];
            [item addSubview:secureButton];
            [secureButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(item).offset(-5 * kAutoScanleX);
                make.centerY.equalTo(item);
                make.size.mas_equalTo(CGSizeMake(44 * kAutoScanleX, 44 * kAutoScanleX));
            }];
            
        }
        
        
        if (i == 3) {
            
            makePassTextField = textField;
            [textField setSecureTextEntry:YES];
            
            UIButton * secureButton = [[UIButton alloc]init];
            [secureButton setImage:[UIImage imageNamed:@"icon_mima_guanbi"] forState:UIControlStateNormal];
            [secureButton setImage:[UIImage imageNamed:@"icon_mima_zhankai"] forState:UIControlStateSelected];
            [secureButton addTarget:self action:@selector(buttonMakeSecureAction:) forControlEvents:UIControlEventTouchUpInside];
            [item addSubview:secureButton];
            [secureButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(item).offset(-5 * kAutoScanleX);
                make.centerY.equalTo(item);
                make.size.mas_equalTo(CGSizeMake(44 * kAutoScanleX, 44 * kAutoScanleX));
            }];
        }
        
        
        if (i != 3) {
            
            UIView *line = [[UIView alloc]init];
            [line setBackgroundColor:HexRGBA(0xcfd4db, 1)];
            [item addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.left.right.equalTo(item);
                make.height.mas_equalTo(0.5);
            }];
        }
    }
    
    UIButton * registerBtn=[[UIButton alloc]init];
    [registerBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [registerBtn addTarget:self action:@selector(registerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [registerBtn.layer setMasksToBounds:YES];
    [registerBtn.layer setCornerRadius:8.0 * kAutoScanleX];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [registerBtn.titleLabel setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [vContainer addSubview:registerBtn];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midView.mas_bottom).offset(30 * kAutoScanleX);
        make.height.mas_equalTo(45 * kAutoScanleX);
        make.centerX.equalTo(vContainer);
        make.width.mas_equalTo(576/2.0 * kAutoScanleX);
    }];

    
    [vContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(registerBtn).offset(30 * kAutoScanleX);
    }];
    
}

#pragma mark--弹起键盘
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}


#pragma mark--弹起键盘
- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardF.size.height;
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view).offset(-height);
        }];
        
        [self.view layoutIfNeeded];
        
    }];
    
    
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view);
        }];
        
        [self.view layoutIfNeeded];
        
    }];
    
    
}

#pragma mark -- 完成
- (void)finishBtnAction:(UIButton *)sender{
    
    [self.view endEditing:YES];
    
}

#pragma mark--获取验证码
-(void)buttonAction:(UIButton *)sender{
    
    
    [ApplicationDelegate.window endEditing:YES];
    
    if (phoneTextField.text.length!=11) return [self showHint:@"请核对您的手机号"];
    
    WS(weakSelf);
    
    [weakSelf setAuthCodeSuccess];
    
    
    [HttpRequestHelper requestApiAuthCodeMobile:phoneTextField.text check:1 Block:^(NSString *code, NSString *msg) {
        
        if (REQUEST_SUCCESS) {
            
            NSLog(@"code : %@",code);
            
        }else{
            
            [weakSelf showHint:msg];
        }
    }];
    
}

-(void)setAuthCodeSuccess{
    
    count = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:nil repeats:YES];
    
//    [self showHint:@"验证码已经发送到您的手机"];
    
}

#pragma mark--定时器
-(void)reduceTime:(NSTimer *)_timer{
    
    count--;
    
    count == 0 ? [self labelNormal] : [self labelSelected];
    
}
-(void)labelNormal{
    
    [label setText:@"获取验证码"];
    [codeBtn setUserInteractionEnabled:YES];
    [timer invalidate];
    
}
-(void)labelSelected{
    
    
    [label setText:[NSString stringWithFormat:@"%ld s",count]];
    [codeBtn setUserInteractionEnabled:NO];
    
}

#pragma mark -- 密码是否带星
- (void)buttonSecureAction:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    [passTextField setSecureTextEntry:!sender.selected];
    
}

#pragma mark -- 确认密码是否带星
- (void)buttonMakeSecureAction:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    [makePassTextField setSecureTextEntry:!sender.selected];
    
}

#pragma mark -- 确认修改
- (void)registerBtnAction:(UIButton *)sender{
    
    [self.view endEditing:YES];
    
    if (phoneTextField.text.length != 11) return [self showHint:@"请核对您的手机号"];
    if (codeTextField.text.length == 0)  return [self showHint:@"请填写验证码"];
    if (passTextField.text.length == 0) return[self showHint:@"请输入密码"];
    if (makePassTextField.text.length == 0) return[self showHint:@"请确认输入密码"];
    
    
    NSString * phone = phoneTextField.text;
    NSString * code = codeTextField.text;
    NSString * pass = passTextField.text;
    NSString * makePass = makePassTextField.text;
    
    if (![pass isEqualToString: makePass]) {
        
        return [self showHint:@"两次密码输入不一致"];
    }
    
    
    WS(weakSelf);
    [self showHudInView:self.view hint:@""];
    [HttpRequestHelper requestApiLoginChpwdMobile:phone repassword:makePass password:pass vcode:code Block:^(NSString *msg, NSString *infoMsg) {
        
        [weakSelf hideHud];
        
        if (REQUEST_SUCCESS) {
            
            [weakSelf backAction:nil];
            [weakSelf showHint:infoMsg];
            
        }else{
            
            [weakSelf showHint:msg];
        }
        
    }];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    
    return YES;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    
}


@end
